import Vue from 'vue'
import App from '@/App.vue'
import router from '@/router'
import store from '@/store'
import { eventBusPlugin } from '@web-utils/vue/mixin/bus'
import { capitalize, dateTime, formatSize, friendlyTime, multiLine } from '@web-utils/vue/filters'
import { clickOutside, dialogDrag, fixFixedColumnHeight, scrollBottom } from '@web-utils/vue/directives'
import fixElementUI from '@/vue/plugins/fix-element-ui'
import { isIE, isBlank, isEmpty } from '@web-utils/core'
import ElementUI from '@/util/use-element-ui'
import WebComponent from '@/util/use-web-component'
import { ImagePreview, ImageCropper } from '@web-utils/component'
import {
  validateId,
  openTab,
  closeTab,
  reloadTableView,
  refreshTreeView,
  refreshHome,
  refreshWork,
  patchTarget,
  setNullTarget
} from '@/vue/plugins'
import { dateFormat, formatDate } from '@/util/date'
import { downBlobCustomeFile, downBlobFile, loadTheme } from '@/util'
import { initApp, server } from '@/config'
import { updateTheme } from '@/mixins/color'
import ErrorHandler from '@/util/error'
import { http } from '@/http'
import { withContext } from '@/util/core'

if (isIE) {
  location.href = `${server}support.html`
}

Vue.config.productionTip = process.env.NODE_ENV !== 'production'

const defaultOption = {
  size: 'small',
  calcHeight: 0,
  menuType: 'text',
  modalAppendToBody: true,
  appendToBody: true,
  canvas: {
    text: 'fy.com',
    fontFamily: 'microsoft yahei',
    color: '#999',
    fontSize: 16,
    opacity: 100,
    bottom: 10,
    right: 10,
    ratio: 1
  },
  qiniu: {
    AK: '',
    SK: '',
    scope: '',
    url: '',
    bucket: 'https://upload.qiniup.com',
    deadline: 1
  },
  ali: {
    region: '',
    endpoint: '',
    stsToken: '',
    accessKeyId: '',
    accessKeySecret: '',
    bucket: ''
  }
}

Vue.prototype.$axios = http
Vue.prototype.$AVUE = defaultOption
Vue.prototype.$ImagePreview = ImagePreview.imagePreview
Vue.prototype.$ImageCropper = ImageCropper.imageCropper
Vue.prototype.$isBlank = isBlank
Vue.prototype.$isEmpty = isEmpty
Vue.prototype.$downBlobCustomeFile = downBlobCustomeFile
Vue.prototype.$downBlobFile = downBlobFile
Vue.prototype.$dateFormat = dateFormat
Vue.prototype.$formatDate = formatDate
Vue.prototype.$withContext = withContext

Vue.filter('capitalize', capitalize)
Vue.filter('dateTime', dateTime)
Vue.filter('formatSize', formatSize)
Vue.filter('friendlyTime', friendlyTime)
Vue.filter('multiLine', multiLine)

Vue.directive('dialog-drag', dialogDrag)
Vue.directive('scroll-bottom', scrollBottom)
Vue.directive('fix-fixed-column-height', fixFixedColumnHeight)
Vue.directive('click-outside', clickOutside)

if (process.env.NODE_ENV !== 'production') {
  Vue.use(ErrorHandler)
}

Vue.use(ElementUI)
Vue.use(WebComponent)
Vue.use(eventBusPlugin)
Vue.use(fixElementUI)
Vue.use(patchTarget)
Vue.use(setNullTarget)
Vue.use(validateId)
Vue.use(openTab)
Vue.use(closeTab)
Vue.use(reloadTableView)
Vue.use(refreshTreeView)
Vue.use(refreshHome)
Vue.use(refreshWork)
;(async () => {
  loadTheme(store.state.viewState.theme)
  await updateTheme(store.state.viewState.themeColor)

  await initApp()

  store.commit('viewState/adjustSidebar')

  // 以下页面不需要动态添加菜单路由
  const whiteList = ['/login', '/ssoLogin', '/404', '/403', '/500']
  // 其他页面需要动态添加菜单路由,不然会出现 404
  if (!whiteList.some(value => window.location.hash.indexOf(value) > -1)) {
    try {
      await store.dispatch('user/getMenu')
    } catch (e) {
      console.error(e)
    }
  }

  const app = new Vue({
    router,
    store,
    render: h => h(App)
  })

  router.onReady(() => app.$mount('#app'))
})()
